<?php

namespace app\common\validate\merchant;
use app\common\model\Merchant\MerchantModel;
use app\common\model\Merchant\MerchantShopModel;
use app\common\model\setting\SettingStoreFlagModel;
use app\common\model\setting\SettingStorePlatformModel;
use app\common\model\setting\SettingStoreTypeModel;
use think\Validate;
/**
 * 店铺管理验证器
 */
class MerchantShopValidate extends Validate
{
    // 验证规则
    protected $rule = [
        'ids' => ['require', 'array'],
        'id' => 'require',
        'title' => ['require', 'checkExisted'],
        'merchant_id' => ['require', 'checkMerchantExisted'],
        'setting_store_platform_id' => ['require', 'checkPlatformExisted'],
        'is_assistant' => ['require', 'in' => '0,1'],
        'is_mer_rebate' => ['require', 'in' => '0,1'],
        'is_disable' => ['require', 'in' => '0,1'],
        'auth_status' => ['require', 'in' => '0,1,2'],
        'setting_store_type_id' => ['nullable', 'checkTypeExisted'],
        'setting_store_flag_id' => ['checkFlagExisted'],
        'shop_group_id'=> 'require',
    ];
    // 错误信息
    protected $message = [
        'title.require'          => '请输入店铺名称',
        'merchant_id.require'          => '请选择所属商户',
        'setting_store_platform_id.require'          => '请选择店铺平台',
        'is_assistant.require' => '请选择小助手',
        'is_mer_rebate.require' => '请选择商家返利',
        'auth_status.require' => '请选择审核状态',
        'shop_group_id.require' => '请选择店铺分组',
    ];
    // 验证场景
    protected $scene = [
        'info'    =>  ['id'],
        'add'     =>  ['title', 'merchant_id', 'setting_store_platform_id', ],
        'edit'    =>  ['id', 'title', 'merchant_id', 'setting_store_platform_id', ],
        'dele'    =>  ['ids'],
        'disable' =>  ['ids', 'is_disable'],
        'assistant' =>  ['ids', 'is_assistant'],
        'mer_rebate' =>  ['ids', 'is_mer_rebate'],
        'auth' =>  ['ids', 'auth_status'],
        'flagConfig' =>  ['id', 'setting_store_flag_id'],
        'editGroup'=>  ['ids', 'shop_group_id'],
    ];

    //店铺平台效验
    protected function checkPlatformExisted($value, $rule, $data = [])
    {
        $model = new SettingStorePlatformModel();
        $pk = $model->getPk();
        $where[] = [$pk, '=', $data['setting_store_platform_id']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if (!$info) {
            return '店铺平台不存在';
        }
        return true;
    }
    //店铺类目效验
    protected function checkTypeExisted($value, $rule, $data = [])
    {
        $model = new SettingStoreTypeModel();
        $pk = $model->getPk();
        $where[] = [$pk, '=', $data['setting_store_type_id']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if (!$info) {
            return '店铺类目不存在';
        }
        return true;
    }
    //店铺旗帜效验
    protected function checkFlagExisted($value, $rule, $data = [])
    {
        $model = new SettingStoreFlagModel();
        $pk = $model->getPk();
        $where[] = [$pk, '=', $data['setting_store_flag_id']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if (!$info) {
            return '店铺旗帜不存在';
        }
        return true;
    }
    //商家效验
    protected function checkMerchantExisted($value, $rule, $data = [])
    {
        $model = new MerchantModel();
        $pk = $model->getPk();
        $where[] = [$pk, '=', $data['merchant_id']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if (!$info) {
            return '所属商家不存在';
        }
        return true;
    }
    /**
     * 店铺名称效验
     * @param $value
     * @param $rule
     * @param $data
     * @return string|true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function checkExisted($value, $rule, $data = [])
    {
        $model = new MerchantShopModel();
        $pk = $model->getPk();
        $id = $data[$pk] ?? 0;

        $where[] = [$pk, '<>', $id];
        $where[] = ['title', '=', $data['title']];
        $where = where_delete($where);
        $info = $model->field($pk)->where($where)->find();
        if ($info) {
            return '名称已存在：' . $data['title'];
        }

        return true;
    }
}
